import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

class LYHttpServer {
  late HttpServer service;
  // 启动服务
  startService() async {
    // 启动 HttpService
    service = await HttpServer.bind(InternetAddress.anyIPv4, 25210);
    // 这种获取方式不准 只能获取到0.0.0.0
    debugPrint("服务器访问地址：${service.address.address}:25210");
    // 监听所有Http请求
    // service.listen((request){
    //
    // });
    await service.forEach((HttpRequest request) async {
      if (request.uri.path == '/test') {
        // 入口文件
        request.response
          ..statusCode = HttpStatus.ok
          ..headers.contentType = ContentType.html
          ..write("111111112222")
          ..close();
      } else {
        // 其他请求都是404
        request.response
          ..statusCode = HttpStatus.notFound
          ..close();
      }
    });
  }

  //关闭服务
  closeService() {
    service.close();
  }
}
